﻿using SiteApp.BLL.Interface;
using SiteApp.DAL;
using SiteApp.Model;
using System;
using System.Collections.Generic;
using System.Linq;

namespace SiteApp.BLL
{
    public class PointService: IPointService
    {



        /// <summary>
        /// 模糊匹配查询设备
        /// </summary>
        public List<PointDetails> SearchPoint(string type, string siteId, string voltage, string device)
        {
            var data = new List<SqlPoint>();
            var pointDetails = new List<PointDetails>();
            if (!string.IsNullOrWhiteSpace(type) || !string.IsNullOrWhiteSpace(siteId) ||
                !string.IsNullOrWhiteSpace(voltage) || !string.IsNullOrWhiteSpace(device))
            {
                var sql = $"select a.* from [Point] as a  inner join  Device as b on a.DeviceId=b.[SID] inner join[Site] as c on c.[SID]= b.SiteId  where c.[SID]={siteId}";

                if (!string.IsNullOrWhiteSpace(type))
                {
                    if (type=="3")
                    {
                        sql += $" and  (a.[Type]='{type}' or a.[Type]=4) ";

                    }
                    else
                    {
                        sql += $" and  a.[Type]='{type}' ";
                    }
             
                }

                if (!string.IsNullOrWhiteSpace(voltage))
                {
                    sql += $" and  a.[voltage]='{voltage}' ";
                }

                if (!string.IsNullOrWhiteSpace(device))
                {
                    sql += $" and  a.[DeviceId]='{device}' ";
                }
                data = SqlDBHelper.Query<SqlPoint>(sql);

                //老红外和温湿度,SF6
                if(data.Where(a => a.Type< 4).Count() > 0)
                {
                    var detailsList = PointDetailsForType(Convert.ToInt32(type), sql, new { @Point = data.Where(a => a.Type < 4).Select(a => a.SID).ToList() });
                    pointDetails.AddRange(detailsList);
                }
                
                //新红外
                if (data.Where(a=>a.Type==4).Count()>0)
                {
                    sql = "select * from [NewInfrared] where [DeviceSn] in @Point";
            
                    var  newInfrared = SqlDBHelper.Query<NewInfrared>(sql, new { @Point = data.Where(a => a.Type == 4).Select(a => a.SID).ToList() });
                    
                    if (newInfrared!=null && newInfrared.Count>0)
                    {
                        pointDetails.AddRange(newInfrared.Select(a => new PointDetails
                        {
                          PointId=a.DeviceSn,
                          DeviceName=a.PointName,
                          Img=a.ImgBase64,
                          CREATETIME=a.CREATETIME
                        }).ToList());
                    }
                    
                }
               
                for (int i = 0; i < pointDetails.Count; i++)
                {
                    pointDetails[i].DeviceName = data.Where(a => a.SID == Convert.ToInt32(pointDetails[i].PointId)).FirstOrDefault().Name;
                    if (!string.IsNullOrWhiteSpace(pointDetails[i].Pressure))
                    {
                        pointDetails[i].Pressure = (Math.Round(Convert.ToDouble(pointDetails[i].Pressure) / 1000, 2) - 1).ToString();
                    }
                    if (!string.IsNullOrWhiteSpace(pointDetails[i].Moisture))
                    {
                        pointDetails[i].Moisture = (Math.Round(Convert.ToDouble(pointDetails[i].Moisture) / 1000, 2) - 1).ToString();
                    }

                }
            }
            return pointDetails;
        }

        private List<PointDetails> PointDetailsForType(int type,string sql, object param = null)
        {
            //点位数据详细信息 可能独立库或者表。分开查询
            switch (type)
            {
                case 1:
                    sql = "select * from [PointForTemperature] where [PointId] in @Point";
                    break;
                case 2:
                    sql = "select * from [PointForSF6] where [PointId] in @Point";
                    break;
                case 3:
                    sql = "select * from [PointForInfrared] where [PointId] in @Point";
                    break;
            }
            return SqlDBHelper.Query<PointDetails>(sql, param);

        }
        public List<PointDetails> PointForDate(string point, string startTime, string endTime,string type)
        {
            var sql = string.Empty;
            switch (Convert.ToInt32(type))
            {
                case 1:
                    sql = $"  select   b.* from  [Point] as a inner join [PointForTemperature]  as b  on a.SID=b.PointId where a.SID = '{point}' and(b.[Createtime] >= '{startTime}' or    b.[Createtime] < '{endTime}')  ";
                    break;
                case 2:
                    sql = $"  select   b.* from  [Point] as a inner join [PointForSF6]  as b  on a.SID=b.PointId where a.SID = '{point}' and(b.[Createtime] >= '{startTime}' or    b.[Createtime] < '{endTime}')";
                    break;
                case 3:
                    sql = $"   select   b.* from  [Point] as a inner join [PointForInfrared]  as b  on a.SID=b.PointId where a.SID = '{point}' and(b.[Createtime] >= '{startTime}' or    b.[Createtime] < '{endTime}')";
                    break;
            }
            var data = SqlDBHelper.Query<PointDetails>(sql);
            return data;
        }


        public SqlPoint GetPoint(string pointId)
        {
            var data = SqlDBHelper.Query<SqlPoint>($"select   * from [Point]  where   [SID]='{pointId}'  ").FirstOrDefault();

            return data;

        }
      




    }
}
